package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api(tags = "医院设置管理接口")
//@CrossOrigin
public class HospitalSetController {
    //注入service层
    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询医院设置的信息
     * @return
     */
    @GetMapping("findAll")
    @ApiOperation(value = "获取分页列表")
    public Result findAllHospitalSet(){
        //调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * 逻辑删除医院设置
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation(value = "逻辑删除医院设置")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    /**
     * 条件查询带分页
     * @param current
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @PostMapping("findPageHospSet/{current}/{limit}")
    @ApiOperation(value = "条件查询")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建page对象，传递当前页，每页的记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构造条件
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }
        //调用方法首先分页查询
        Page<HospitalSet> pageHospSet = hospitalSetService.page(page, queryWrapper);
        return Result.ok(pageHospSet);
    }


    /**
     * 添加医院设置
     * @param hospitalSet
     * @return
     */
    @PostMapping("saveHospitalSet")
    @ApiOperation(value = "添加医院设置")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){

        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥 采用MD5加密方式，service_util中添加了工具类
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+
                random.nextInt(1000)));
        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return Result.ok();
        }
        return Result.fail();
    }


    /**
     * 根据id查询医院设置
     * @param id
     * @return
     */
    @GetMapping("getHospSet/{id}")
    @ApiOperation(value = "根据id查询医院设置")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet hospitalSetId = hospitalSetService.getById(id);
        return Result.ok(hospitalSetId);
    }


    /**
     * 修改医院设置
     * @param hospitalSet
     * @return
     */
    @PutMapping("updateHospitalSet")
    @ApiOperation(value = "修改医院设置")
    public Result updateHospitalSet(@PathVariable HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }


    /**
     * 批量删除医院设置
     * @param idList
     * @return
     */
    @DeleteMapping("batchRemove")
    @ApiOperation(value ="批量删除医院你设置" )
    public Result batchRemoveHospitalSet(@PathVariable List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 医院设置锁定与解锁
     * @param id
     * @param status
     * @return
     */
    @PutMapping("lockHospitalSet/{id}/{status}")
    @ApiOperation(value = "医院设置锁定与解锁")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        //根据Id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //使用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    /**
     * 发送签名秘钥
     * @param id
     * @return
     */
    @PutMapping("sendKey/{id}")
    @ApiOperation(value = "发送签名秘钥")
    public Result lockHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hosname = hospitalSet.getHosname();
        String hoscode = hospitalSet.getHoscode();

        //发送短信
        return Result.ok();
    }

}